package service;

import java.util.ArrayList;

import org.apache.ibatis.session.SqlSession;

import bean.Address;
import mapper.AddressMapper;
import util.MybatisUtil;

public class AddressServiceImpl implements AddressService{
	@Override
	public ArrayList<Address> deleteAddress(String userAddress) {
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		AddressMapper addressMapper = sqlSession.getMapper(AddressMapper.class);
		int result = addressMapper.deleteAddress(userAddress);
		sqlSession.commit();
		ArrayList<Address> newAddress = addressMapper.getAllAddress();
		return newAddress;
	}

	@Override
	public int getAddressCount() {
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		AddressMapper addressMapper = sqlSession.getMapper(AddressMapper.class);
		int addressnum = addressMapper.getAddressNum();
		return addressnum;
	}

	@Override
	public ArrayList<Address> insertAddress(Address address) {
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		AddressMapper addressMapper = sqlSession.getMapper(AddressMapper.class);
		int result = addressMapper.insertAddress(address);
		sqlSession.commit();
		ArrayList<Address> allAddress = addressMapper.getAllAddress();
		return allAddress;
		
	}

	@Override
	public int updateAddressByName(Address address) {
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		AddressMapper addressMapper = sqlSession.getMapper(AddressMapper.class);
		int result = addressMapper.updateAddressByName(address);
		sqlSession.commit();
		return result;
	}

	@Override
	public Address getAddressByName(String name) {
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		AddressMapper addressMapper = sqlSession.getMapper(AddressMapper.class);
		Address addressByName = addressMapper.getAddressByName(name);
		return addressByName;
	}
	
}
